package com.katze.boot.quartz.controller;

import com.katze.boot.common.exception.BusinessException;
import com.katze.boot.plugins.security.annotations.RestControllerMapping;
import com.katze.boot.plugins.security.annotations.RestRequestMapping;
import com.katze.boot.quartz.domain.Job;
import com.katze.boot.quartz.service.JobService;
import com.katze.common.Message;
import com.katze.common.Pagination;
import com.katze.common.controller.BaseController;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import org.quartz.CronExpression;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.List;

@RestControllerMapping("/job")
public class JobController extends BaseController {

    @Resource
    private JobService jobService;

    @RestRequestMapping(permits = "job:view")
    public Message jobList(Pagination request, Job job) {
        return toDataTable(this.jobService.findJobs(request, job));
    }

    @RestRequestMapping(value = "/check/cron", skip = true)
    public Message checkCron(String cron) {
        return Message.ok().data(CronExpression.isValidExpression(cron));
    }

    @RestRequestMapping(method = RequestMethod.POST, permits = "job:view", description = "新增定时任务")
    public Message addJob(Job job) {
        this.jobService.createJob(job);
        return Message.ok("定时任务已新增成功");
    }

    @RestRequestMapping(method = RequestMethod.DELETE, permits = "job:delete", description = "删除定时任务")
    public Message deleteJob(String... jobIds) {
        this.jobService.deleteJobs(jobIds);
        return Message.ok("删除定时任务成功");
    }

    @RestRequestMapping(method = RequestMethod.PUT, permits = "job:update", description = "修改定时任务")
    public Message updateJob(Job job) {
        this.jobService.updateJob(job);
        return Message.ok("定时任务修改成功");
    }

    @RestRequestMapping(value = "/run/{jobId}", permits = "job:run", description = "执行定时任务")
    public Message run(@PathVariable String jobId) {
        this.jobService.run(jobId);
        return Message.ok("定时任务开始执行");
    }

    @RestRequestMapping(value = "/pause/{jobId}", permits = "job:pause", description = "暂停定时任务")
    public Message pauseJob(@PathVariable String jobId) {
        this.jobService.pause(jobId);
        return Message.ok("定时任务已暂停");
    }

    @RestRequestMapping(value = "/resume/{jobId}", permits = "job:resume", description = "恢复定时任务")
    public Message resumeJob(@PathVariable String jobId) {
        this.jobService.resume(jobId);
        return Message.ok("定时任务已恢复");
    }

    @RestRequestMapping(value = "/excel", method = RequestMethod.POST, permits = "job:export")
    public void export(Pagination request, Job job, HttpServletResponse response) throws BusinessException {
        try {
            List<Job> jobs = this.jobService.findJobs(request, job).getRecords();
            super.downXlsx("定时任务信息表", jobs, response);
        } catch (Exception e) {
            throw new BusinessException("导出Excel失败", e);
        }
    }
}
